package gui.panels.statistics;

import gui.GuiManager;
import gui.GuiManager.FramesEnum;
import gui.components.AntiAliasedLabel;
import gui.components.FancyButton;
import gui.panels.CopyrightPanel;
import gui.panels.CopyrightPanel.ButtonTypeEnum;
import gui.utils.ComponentUtils;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.border.EmptyBorder;

import common.models.StatisticsModel;

public class StatisticsPanel extends JPanel {

    private static final long serialVersionUID = 1L;
    
    private static final int COMMON_GAP = 12;
    
    private JLabel title;
    private JPanel content;
    private MostPlayedPanel mostPlayed;
    private GameHistoryPanel gameHistory;
    private SuccessPiePanel successPie;
    
    @SuppressWarnings("serial")
    public StatisticsPanel(StatisticsModel model) {
        
        setLayout(new BorderLayout());
        setBackground(Color.WHITE);
        
        title = new AntiAliasedLabel(null, 22) {{
            setAlignmentX(CENTER_ALIGNMENT);
        }};
        
        content = new JPanel() {{
           setAlignmentX(CENTER_ALIGNMENT);
           setBackground(Color.WHITE);
        }};
        
        mostPlayed = new MostPlayedPanel(model.getMostPlayed());
        gameHistory = new GameHistoryPanel(model.getGameHistory());
        successPie = new SuccessPiePanel(model.getSuccesfulGuesses(), model.getTotalGuesses());
        
        add(buttons(), BorderLayout.WEST);
        add(center(), BorderLayout.CENTER);
        add(bottom(), BorderLayout.SOUTH);
        
        switchContent(mostPlayed);
    }

    @SuppressWarnings("serial")
    private JComponent buttons() {
        return new Box(BoxLayout.Y_AXIS) {{
            add(statisticsButton(mostPlayed));
            add(Box.createVerticalStrut(COMMON_GAP));
            add(statisticsButton(gameHistory));
            add(Box.createVerticalStrut(COMMON_GAP));
            add(statisticsButton(successPie));
            setBorder(new EmptyBorder(COMMON_GAP,COMMON_GAP,0,0));            
        }};
    }
    
    private JPanel statisticsButton(final StatisticsContent content) {
        
        Color buttonColor = new Color(235,235,235);
        
        FancyButton button = new FancyButton(
                ComponentUtils.readImageIcon(content.getIconName()), 
                content.getTitle(), buttonColor, buttonColor);
        
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                SwingUtilities.invokeLater(new Runnable() {
                    public void run() {
                        switchContent(content);
                    }
                });
            }
        });
        
        return button;
    }
    
    private void switchContent(StatisticsContent newContent) {
        title.setText(newContent.getTitle());
        content.removeAll();
        content.add(newContent.getContent());
        content.repaint();
    }
    
    @SuppressWarnings("serial")
    private JComponent center() {
        return new Box(BoxLayout.Y_AXIS) {{
            add(title);
            add(Box.createVerticalStrut(COMMON_GAP));
            add(content);
            setBorder(new EmptyBorder(COMMON_GAP*2,COMMON_GAP*2, 0 ,COMMON_GAP*2));
        }};
    }
    
    private JComponent bottom() {    
        CopyrightPanel copyrightPanel = new CopyrightPanel(ButtonTypeEnum.BACK_BUTTON, new ActionListener() {	
			@Override
			public void actionPerformed(ActionEvent arg0) {
				GuiManager.switchFrame(FramesEnum.PREVIOUS_FRAME);
			}
		});
        copyrightPanel.setBorder(BorderFactory.createEmptyBorder(0,0,0,0));
        
        return copyrightPanel;
    }
    
}


